#!/usr/bin/env node
const fs = require('fs'),
      http = require('http'),
      path = require('path'),
      file = process.argv[2];

if(process.argv.length !== 3){
  console.error('命令行格式：cmd fileName');
  process.exit(1);
}

try{
  var data = fs.readFilesync(file).toString('base64');
}catch(e){
  console.error(e.message);
  process.exit(2);
}

let ext = path.extname(file);
let uriData = 'data:image/' + ext.slice(1,ext.length) + ';base64,' + data;

let html = `<img src="${uriData}">`;

http.createServer((req,res) => {
  console.log(req.headers);
  console.log(req.url + '\n');
  res.end(html);
}).listen(8080);
